package com.bysj.money_diary;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.PropertyValuesHolder;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener, android.view.View.OnClickListener{

	private TextView mBtnLogin,login_register,login_shualian;
	private View progress;
	private View mInputLayout;
	private float mWidth, mHeight;
	private LinearLayout mName, mPsw;
	private EditText login_name = null,login_paw = null;
	public Data dataHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		
		initView();
	}

	private void initView() {
		mBtnLogin = (TextView) findViewById(R.id.login_btn_main);
		progress = findViewById(R.id.login_progress);
		mInputLayout = findViewById(R.id.login_input);
		login_register = (TextView)findViewById(R.id.login_register);
		login_shualian = (TextView)findViewById(R.id.login_shualian);
		mName = (LinearLayout) findViewById(R.id.input_layout_name);
		mPsw = (LinearLayout) findViewById(R.id.input_layout_psw);
		login_name = (EditText)findViewById(R.id.login_name);
		login_paw = (EditText)findViewById(R.id.login_paw);

		mBtnLogin.setOnClickListener(this);
		login_register.setOnClickListener(this);
		login_shualian.setOnClickListener(this);
	}


	public void onClick(View v) {
		mWidth = mBtnLogin.getMeasuredWidth();
		mHeight = mBtnLogin.getMeasuredHeight();
		mName.setVisibility(View.INVISIBLE);
		mPsw.setVisibility(View.INVISIBLE);

		inputAnimator(mInputLayout, mWidth, mHeight);
		
		
		switch(v.getId()){
		case R.id.login_btn_main://登录
			login(); 
			break;
		case R.id.login_register://注册
			Intent intent1 = new Intent();
			intent1.setClass(LoginActivity.this, RegisterActivity.class);  
			startActivity(intent1);
			break;
		case R.id.login_shualian://刷脸
			Intent intent2 = new Intent();
			intent2.setClass(LoginActivity.this, ShualianActivity.class);  
			startActivity(intent2);
			break;
		
		default:
			break;
		}
	}

	private void login() {
		if (isUserNameAndPwdValid()) {  
			dataHelper = new Data(this);
            String userName = login_name.getText().toString().trim();    
            String userPwd = login_paw.getText().toString().trim();  
            int result = dataHelper.UserbyNameAndPwd(userName, userPwd);
            if(result==1){                                                
                Intent intent = new Intent(LoginActivity.this,MainActivity.class) ;      
                startActivity(intent);  
                finish();  
                Toast.makeText(this, getString(R.string.login_success),Toast.LENGTH_SHORT).show();//��¼�ɹ���ʾ  
            }else if(result==0){  
                Toast.makeText(this, getString(R.string.loginpwd_fail),Toast.LENGTH_SHORT).show();  //��¼ʧ����ʾ  
            }  
        }  
		
	}

	private boolean isUserNameAndPwdValid() {
		if (login_name.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.account_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;  
        } else if (login_paw.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.pwd_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;  
        }  
        return true;
	}

	@SuppressLint("NewApi")
	private void inputAnimator(final View view, float w, float h) {
		ValueAnimator animator = ValueAnimator.ofFloat(0, w);
		animator.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				float value = (Float) animation.getAnimatedValue();
				ViewGroup.MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
				params.leftMargin = (int) value;
				params.rightMargin = (int) value;
				view.setLayoutParams(params);
			}
		});
		AnimatorSet set = new AnimatorSet();
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(mInputLayout, "scaleX", 1f, 0.5f);
		set.setDuration(1000);
		set.setInterpolator(new AccelerateDecelerateInterpolator());
		set.playTogether(animator, animator2);
		set.start();
		set.addListener(new AnimatorListener() {

			public void onAnimationStart(Animator animation) {

			}

			public void onAnimationRepeat(Animator animation) {
				

			}

			public void onAnimationEnd(Animator animation) {

				progress.setVisibility(View.VISIBLE);
				progressAnimator(progress);
				mInputLayout.setVisibility(View.INVISIBLE);

				new Handler().postDelayed(new Runnable() {
					public void run() {
						recovery();
					}
				}, 2000);
			}

			public void onAnimationCancel(Animator animation) {
				

			}
		});
	}

	private void progressAnimator(final View view) {
		PropertyValuesHolder animator = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 1f);
		PropertyValuesHolder animator2 = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 1f);
		ObjectAnimator animator3 = ObjectAnimator.ofPropertyValuesHolder(view, animator, animator2);
		animator3.setDuration(1000);
		animator3.setInterpolator(new JellyInterpolator());
		animator3.start();

	}


	private void recovery() {
		progress.setVisibility(View.GONE);
		mInputLayout.setVisibility(View.VISIBLE);
		mName.setVisibility(View.VISIBLE);
		mPsw.setVisibility(View.VISIBLE);

		ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mInputLayout.getLayoutParams();
		params.leftMargin = 0;
		params.rightMargin = 0;
		mInputLayout.setLayoutParams(params);
		
		
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(mInputLayout, "scaleX", 0.5f,1f );
		animator2.setDuration(500);
		animator2.setInterpolator(new AccelerateDecelerateInterpolator());
		animator2.start();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		
		
	}
}
